#ifndef LFOSC2INTERFACE_H
#define LFOSC2INTERFACE_H

#include "LFOSCommon.h"
#include "EdgeDetection.h"
#include "LineExtraction.h"

#define C2THETASTEP 2
#define C2THETASIZE 90
#define C2THETAMIN -88
#define C2THETAMAX 90
#define C2TRIGFACTOR 1000

enum DetectionType{Obstruction = 1, IntersectionReached = 2, IntersectionAhead = 4, LineDetection = 8};

typedef struct{
	//Can take 1 or more values of DetectionType
	int type;

	//only valid when DetectionType = IntersectionReached or
	//DetectionType = IntersectionAhead
	bool roadLeft;
	bool roadStraight;
	bool roadRight;
	bool drivewayIntersection;

	//only valid when DetectionType=Line
	int fromCenter; //distance from center in pixels
	                //negative - right of line, positive - left of line
	int angle; //angle from line, 0 - parallel to line
	           //negative - line points to top left
	           //positive - line points to top right

	Point intersection;
}DetectionInfo;

class LFOSC2Interface
{
public:
	LFOSC2Interface();
	DetectionInfo ProcessImage(unsigned char* image);

private:
	//Finds an intersection ahead - returns true if reached
	bool FindIntersection(LineSegment* segments, unsigned int numSegments, bool& ahead);
	//Finds obstructions head - returns true if obstruction found
	bool FindObstruction(LineSegment* segments, unsigned int numSegments);
	//Finds the closest line to the center of the road
	void FindClosestLine(LineSegment* segments, unsigned int numSegments,
			             int& angle, int& distance);
	bool IsPointOnLineSegment(LineSegment ls, Point p);

	int LineLengthSquare(const LineSegment& ls);

	//Edge Detection and Line Extraction
	EdgeDetection ed;
	LineExtraction le;

	bool checkObstruction;

	//variables to hold state of intersection ahead
	Point intersectionLocation;
	Point obstructionLocation;
	bool drivewayIntersection;
	bool roadLeft;
	bool roadRight;
	bool roadStraight;

	int obstructionSegmentIndex[2];

	static short cos[C2THETASIZE];
	static short sin[C2THETASIZE];

};

#endif
